package com.examples.spider.pipeline;

import com.examples.spider.model.GitBook;
import com.examples.spider.model.GitBookCatalog;
import com.examples.spider.model.GitBookChapter;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.utils.FilePersistentBase;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

/**
 * @author GFL
 * @Date 2018/3/20 18:26.
 */
public class GitBookPipeline extends FilePersistentBase implements Pipeline {

    private Logger logger = LoggerFactory.getLogger(getClass());
    private String CHAPTER_FILES = "chapter";
    private String CONTENT_LINE_BREAK = "    ";

    /**
     * create a FilePipeline with default path"/data/webmagic/"
     */
    public GitBookPipeline() {
        setPath("/data/webmagic/");
    }

    public GitBookPipeline(String path) {
        setPath(path);
    }

    public void process(ResultItems resultItems, Task task) {
        gitBookSummary(resultItems, task);
        gitBookChapter(resultItems, task);
    }

    private void gitBookSummary(ResultItems resultItems, Task task) {
        String path = this.path + PATH_SEPERATOR + task.getUUID() + PATH_SEPERATOR;
        String fileName = path + "SUMMARY" + ".md";
        GitBook gitBook = resultItems.get("catalogs");
        if (null != gitBook) {
            try {
                PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(getFile(fileName)), "UTF-8"));
                printWriter.println("# [" + gitBook.getBookName() + "](" + resultItems.getRequest().getUrl() + ")");
                printWriter.println("## 目录");
                StringBuilder sb = new StringBuilder();
                for (GitBookCatalog catalog : gitBook.getCatalogs()) {
                    sb.setLength(0);
                    sb.append("* ").append("[").append(catalog.getCatalogName()).append("]").append("(").append(CHAPTER_FILES).append(PATH_SEPERATOR).append(DigestUtils.md5Hex(catalog.getCatalogLink())).append(".md)");
                    printWriter.println(sb.toString());
                }
                printWriter.close();
            } catch (IOException e) {
                logger.warn("write file error", e);
            }
        }
    }


    private void gitBookChapter(ResultItems resultItems, Task task) {
        String path = this.path + PATH_SEPERATOR + task.getUUID() + PATH_SEPERATOR + CHAPTER_FILES + PATH_SEPERATOR;
        String fileName = path + DigestUtils.md5Hex(resultItems.getRequest().getUrl()) + ".md";
        GitBookChapter chapter = resultItems.get("chapter");
        if (null != chapter) {
            try {
                PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(getFile(fileName)), "UTF-8"));
                printWriter.println("## [" + chapter.getTitle() + "](" + resultItems.getRequest().getUrl() + ")");
                printWriter.println(chapter.getContent().replace(CONTENT_LINE_BREAK, "\n\n  "));
                printWriter.close();
            } catch (IOException e) {
                logger.warn("write file error", e);
            }
        }
    }

}
